package controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Event;
import models.User;
import models.Event.Category;
import play.mvc.Controller;

public class SearchEngine extends Controller {

	/**************************** Search pages **************************/

	public static void trips() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}

	public static void entertainment() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}

	public static void culture() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}

	public static void sport() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}

	public static void nightlife() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}

	public static void learning() {
		User user = User.find("byEmail", session.get("username")).first();
		render(user);
	}

	/************************** Search page ******************************/

	public static void search(String categoryString) {
		User user = User.find("byEmail", session.get("username")).first();
		renderArgs.put("categoryString", categoryString);
		render(user);
	}
	
	public static void searchresult(String categoryString, String title, String location, String author, Date minDate, Date maxDate) {
		User user = User.find("byEmail", session.get("username")).first();
		Category category = Category.valueOf(categoryString);
		
		String queryStr = "category like ?";
		List<Object> params = new ArrayList<Object>();
		params.add(category);
		if(title!=null && !title.isEmpty()) {
			queryStr += " and title like ?";
			params.add(title);
		}
		if(location!=null && !location.isEmpty()) {
			queryStr += " and location like ?";
			params.add(location);
		}
		if(author!=null && !author.isEmpty()) {
			queryStr += " and author.userName like ?";
			params.add(author);
		}
		if(maxDate!=null && !maxDate.toString().isEmpty()) {
			queryStr += " and eventDate <= ?";
			params.add(maxDate);
		}
		if(minDate!=null && !minDate.toString().isEmpty()) {
			queryStr += " and eventDate >= ?";
			params.add(minDate);
		}
		else {
			queryStr += " and eventDate >= CURRENT_DATE";
		}
		queryStr += " order by eventDate";
		List<Event> events = Event.find(queryStr, params.toArray()).fetch();
		renderArgs.put("categoryString", categoryString);
		render(events, user);
	}
	
}
